<?php
/**
 * Formular (abstraktny)
 * @author Tomas Srna
 * @package bakalarka
 */

abstract class Form
{
	protected $action;
	protected $method;
	
	protected $items = array();
	protected $description = "";
	
	protected $values = array();
	protected $errors = array();
	
	/**
	 * Konstruktor
	 * @param array $values		Hodnoty poli formulara
	 */
	public function __construct(Template $template, $values = NULL, 
		$action = NULL, $method = 'POST')
	{
		$template->addHook('headEnd', "<link rel=\"stylesheet\" ".
			"type=\"text/css\" href=\"".APPBASE."css/form.css\" />");
		
		if($values != NULL)
			$this->values = $values;
		
		if($action)
			$this->action = $action;
		else
			$this->action = getenv("REQUEST_URI");
			
		$this->method = $method;
	}
	
	/**
	 * V abstraktnom formulari vykoname zakladne checky podla class
	 */
	public function validate()
	{
		// Ked uz sme validovali, nevalidujme
		if(count($this->errors) > 0)
			return false;
		
		foreach ($this->items as $element)
		{
			$classes = explode(" ", $element->class);
			
			if(array_search('required', $classes) !== false &&
				($this->getValue($element->name) === "" ||
				 $this->getValue($element->name) === NULL))
			{
				$this->errors[$element->name] = L::selected()->emptyRequiredField;
			}
			else if(array_search('numeric', $classes) !== false &&
				$this->getValue($element->name) &&
				!is_numeric($this->getValue($element->name)))
			{
				$this->errors[$element->name] = L::selected()->mustBeNumeric;
			}
			else if(array_search('nospace', $classes) !== false &&
				$this->getValue($element->name) &&
				strpos($this->getValue($element->name), " ") !== false)
			{
				$this->errors[$element->name] = L::selected()->noSpaces;
			}
			else if(array_search('email', $classes) !== false &&
				$this->getValue($element->name) &&
				filter_var($this->getValue($element->name), 
				FILTER_VALIDATE_EMAIL) === false)
			{
				$this->errors[$element->name] = L::selected()->badEmail;
			}
			else if(array_search('alnum', $classes) !== false &&
				$this->getValue($element->name) &&
				!preg_match('/^[a-z0-9]+$/i', 
					$this->getValue($element->name)))
			{
				$this->errors[$element->name] = L::selected()->alnum;
			}
			else if(array_search('path', $classes) !== false &&
				$this->getValue($element->name) &&
				!preg_match('/^[a-z0-9\.\/_]+$/i', 
					$this->getValue($element->name)))
			{
				$this->errors[$element->name] = sprintf(
						L::selected()->invalidFormat_s,
						'a-z A-Z 0-9 . / _'
					);
			}
			else if(array_search('albegin', $classes) !== false &&
				$this->getValue($element->name) &&
				!preg_match('/^[a-z]+$/i', 
					substr($this->getValue($element->name), 0, 1)))
			{
				$this->errors[$element->name] = L::selected()->albegin;
			}
		}
		
		if(count($this->errors) > 0)
			return false;
		
		return true;
	}
	
	public function getItems()
	{
		return $this->items;
	}
	
	public function getAction()
	{
		return $this->action;
	}
	
	public function getMethod()
	{
		return $this->method;
	}
	
	public function getValues()
	{
		return $values;
	}
	
	public function getValue($name)
	{
		if($this->values instanceof Model && 
			property_exists($this->values, $name))
		{
			return $this->values->{$name};
		}
		else if(array_key_exists($name, $this->values))
			return $this->values[$name];
		else
			return NULL;
	}
	
	public function getError($name)
	{
		if(!array_key_exists($name, $this->errors))
			return "";
		else
			return $this->errors[$name];
	}
	
	public function setError($name, $value)
	{
		$this->errors[$name] = $value;
	}
	
}
 